package com.market.web.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.Product;
import com.market.domain.ProductPrice;
import com.market.domain.Weekly;
import com.market.manager.ProductManager;
import com.market.manager.WeeklyManager;
import com.market.util.CollectionUtil;
import com.market.web.controller.report.ReportController;
import com.market.web.model.MarketList;
import com.market.web.model.ProductModel;
import com.market.web.model.WeeklyModel;

@Controller("weeklyController")
@Scope("session")
public class WeeklyController extends
		GenericController<WeeklyManager, WeeklyModel, Weekly> {
	@Autowired
	private ProductModel productModel;
	@Autowired
	private ProductManager productManager;
	@Autowired
	ReportController reportController;

	@Autowired
	@Override
	protected void setModel(WeeklyModel model) {
		this.model = model;
	}

	@Autowired
	@Override
	protected void setManager(WeeklyManager manager) {
		this.manager = manager;

	}

	/**
	 * This method init and sort weekly to ascendent order; Sort is done by
	 * database's order by clause
	 */
	@Override
	public void init() {
		super.init();
		List<Weekly> weeklys = this.getModel().getList();

		weeklys.set(0, this.fetchOne(weeklys.get(0))); // Fetch all related info
														// of first weekly

		// for (Weekly weekly : this.getModel().getList()) {
		// weekly = this.fetchOne(weekly);
		// weeklys.add(weekly);
		// }
		this.model.setList(weeklys);
	}
	
	
	public void loadInStockNotInCurrentWeeklyForCurrent(){
		this.getModelCurrent().setInStockNotInWeeklyProducts(this.manager.fetchProductsInStockNotInWeekly(this.getModelCurrent()));
	}
	

	public StreamedContent getInStockNotInCurrentWeeklyPdf(){
		return this.reportController.printProduct(this.getModelCurrent().getInStockNotInWeeklyProducts());
	}

	/**
	 * This method init and sort only active weekly Sort is done by database's
	 * order by clause
	 */
	public void initActiveWeekly() {
		List<Weekly> weeklys = this.manager.fetchActiveWeeklys();
		List<Weekly> activeWeeklys = new ArrayList<Weekly>();
		for (Weekly weekly : weeklys) {
			weekly = this.fetchOne(weekly);
			activeWeeklys.add(weekly);
		}
		this.model.setList(activeWeeklys);
	}

	public List<Weekly> fetchActiveWeekly() {
		return this.manager.fetchActiveWeeklys();
	}

	/**
	 * Add all selected products to current weekly And clear selected propertied
	 * after this operation finish
	 * 
	 */
	public void addSelectedProductsToCurrent() {
		Weekly current = this.getModelCurrent();
		List<Product> selectedProducts = productModel.getSelecteds();

		// selectedProducts = productModel.getSelectedList();

		List<ProductPrice> productPrices = current.getProductPrices();
		Boolean exist = Boolean.FALSE;
		for (Product product : selectedProducts) {
			exist = Boolean.FALSE;
			for (ProductPrice productPrice : productPrices) {
				if (product.getId().equals(productPrice.getProduct().getId())) {
					exist = Boolean.TRUE;
					break;
				}
			}
			if (exist == Boolean.FALSE) {
				current.getProductPrices().add(new ProductPrice(product));
			}
		}
		CollectionUtil<Product> productUtil = new CollectionUtil<Product>();
		productUtil.clearSelection(selectedProducts); // Normally we have to
														// clear selected
														// properties, after
														// operation finish
	}

	/**
	 * Remove all selected products from current weekly And clear selected
	 * propertied after this operation finish
	 * 
	 */

	public void removeSelectedProductsFromCurrent() {
		List<ProductPrice> productPrices = this.getModel().getCurrent()
				.getProductPrices();
		CollectionUtil<ProductPrice> util = new CollectionUtil<ProductPrice>();
		// List<ProductPrice> unSelectedProducts = util
		// .extractUnSelected(productPrices);
		// this.getModelCurrent().setProductPrices(unSelectedProducts);

		List<ProductPrice> selectedProducts = util
				.extractSelected(productPrices);
		util.removeAll(this.getModelCurrent().getProductPrices(),
				selectedProducts);//Remove all selected productPrice from current weekly's product price list.
		if (this.getModel().getCurrent().getFilteredProductPrices() != null) { //If datatable is in filtered mode.
			util.removeAll(this.getModel().getCurrent()
					.getFilteredProductPrices(), selectedProducts); //Remove all selected product from filtered productPrice list.
		}
		util.clearSelection(productPrices); // Normally we have to clear
											// selected properties, after
											// operation finish
	}

	public void importFromSelectedWeeks() {
		Weekly currentWeek = this.getModelCurrent();
		for (Weekly weekly : this.getModel().getSelecteds()) {
			for (ProductPrice productPrice : weekly.getProductPrices()) {
				ProductPrice newProductPrice = new ProductPrice();
				newProductPrice.setPrice(productPrice.getPrice());
				newProductPrice.setSpecialPrice(productPrice.getSpecialPrice());
				newProductPrice.setUnitPrice(productPrice.getUnitPrice());
				newProductPrice.setChineseUnitPrice(productPrice
						.getChineseUnitPrice());
				newProductPrice.setFrenchUnitPrice(productPrice
						.getFrenchUnitPrice());
				newProductPrice.setProduct(productPrice.getProduct());
				currentWeek.getProductPrices().add(newProductPrice);
			}
		}
		this.getModel().clearSelection();
	}

	/**
	 * This method store price,specialPrice, unitPrice, chineseUnitPrice,
	 * frenchUnitPrice, Inventory of product into database;
	 */
	public void storeToProductInfo() {
		ProductPrice productPrice = this.getModel().getCurrentProductPrice();
		Product product = productPrice.getProduct();
		product.setPrice(productPrice.getPrice());
		product.setSpecialPrice(productPrice.getSpecialPrice());
		product.setUnitPrice(productPrice.getUnitPrice());
		product.setChineseUnitPrice(productPrice.getChineseUnitPrice());
		product.setFrenchUnitPrice(productPrice.getFrenchUnitPrice());
		productManager.storeInventory(product);
		productManager.storePriceInfo(product);

	}

}
